﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class AB55DIReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        #endregion
        public AB55DIReport()
            : base()
        {
        }
        public AB55DIReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            ABPAUFFKRS = dicParamater["ABPAUFKRS"].ToString().Trim();
            ABPAUFFLFD = dicParamater["ABPAUFLFD"].ToString().Trim();
            ABPAUFFPOS = dicParamater["ABPAUFPOS"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        #region------------------------------Function------------------------



        private void FillInOrderPos(DataTable dtSource)
        {
            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();

                string ORDERNO = "ORDERNO";
                string FACTORY = "FACTORY";
                string ARTICLE = "ARTICLE";
                string WARPQTY = "WARPQTY";
                string YARN = "YARN";
                string COLOR = "COLOR";
                string REQQTY = "REQQTY";
                string ALLOCATE = "ALLOCATE";
                string WARPORDER = "WARPORDER";
                string REMAIN = "REMAIN";
                string ABPSTARTDAT = "ABPSTARTDAT";
                string YARND = "";
                string SPPART = "SPPART";

                int I = 0;
                string J = "00";

                foreach (DataRow dwSource in dtSource.Rows)
                {
                    I++;
                    J = Right("00" + I.ToString(), 2);
                    YARND = dwSource["YARN"].ToString().Trim() + Environment.NewLine + dwSource["DES"].ToString().Trim();
                    CellName = ORDERNO + J;
                    CellValue = dwSource["WEAVINGORDER"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = FACTORY + J;
                    CellValue = dwSource["LSNAME1"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = ARTICLE + J;
                    CellValue = dwSource["ARTICLE"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = WARPQTY + J;
                    CellValue = dwSource["WARPQTY"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = YARN + J;
                    CellValue = YARND;
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = COLOR + J;
                    CellValue = dwSource["COLOR"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = REQQTY + J;
                    CellValue = dwSource["REQUIREMENT"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = ALLOCATE + J;
                    CellValue = dwSource["ALLOCATION"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = WARPORDER + J;
                    CellValue = dwSource["WARPINGORDER"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = REMAIN + J;
                    CellValue = dwSource["REMAIN"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = ABPSTARTDAT + J;
                    CellValue = dwSource["ABPSTARTDAT"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = SPPART + J;
                    CellValue = dwSource["SPPART"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }


        public void GetReport(string fileType)
        {

            var query = from dwABPOS in dsSource.Tables["ABPOS"].AsEnumerable()
                        select dwABPOS;

            //string strSheetName;
            // string TableName = "";
            // int Rown = 0;
            // string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "Weaving";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\" + ABPAUFFKRS + ABPAUFFLFD + ABPAUFFPOS + ".xls";
                        sheetName = ABPAUFFKRS + "_" + ABPAUFFLFD + "_" + ABPAUFFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        CellName = "PrintDate";
                        value = System.DateTime.Today.ToShortDateString();
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "PageNum";
                        value = "1";
                        exAgent.SetCellValue(CellName, value, true);
                        FillInOrderPos(dsSource.Tables["ABPOS"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (strCompany == "HLT")
                {
                    HideNullRow();
                }
                else
                {
                    //HideNullRow();
                }
                exAgent.Save();
                //MessageBox.Show("你是否打开Exclel?",,MessageBoxButtons.YesNo,MessageBoxDefaultButton.Button2);

                exAgent.Close();
            }
        }
        public void GetWeavingReport(string fileType)
        {

            //var query = from dwABPOS in dsSource.Tables["ABPOS"].AsEnumerable()
                     //   select dwABPOS;

            //string strSheetName;
            // string TableName = "";
            // int Rown = 0;
            // string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "Weaving";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\" + ABPAUFFKRS + ABPAUFFLFD + ABPAUFFPOS + "相关织造配纬单.xls";
                        sheetName = ABPAUFFKRS + "_" + ABPAUFFLFD + "_" + ABPAUFFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        CellName = "PrintDate";
                        value = System.DateTime.Today.ToShortDateString();
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "PageNum";
                        value = "1";
                        exAgent.SetCellValue(CellName, value, true);
                        FillInOrderPos(dsSource.Tables["SAMPLEORDER"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (strCompany == "HLT")
                {
                    HideNullRow();
                }
                else
                {
                    //HideNullRow();
                }
                exAgent.Save();
                //MessageBox.Show("你是否打开Exclel?",,MessageBoxButtons.YesNo,MessageBoxDefaultButton.Button2);

                exAgent.Close();
            }
        }
        public void GetSAMPLEReport(string fileType)
        {

            //var query = from dwABPOS in dsSource.Tables["ABPOS"].AsEnumerable()
            //   select dwABPOS;

            //string strSheetName;
            // string TableName = "";
            // int Rown = 0;
            // string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "Weaving";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\" + ABPAUFFKRS + ABPAUFFLFD + ABPAUFFPOS + "织造试纬单.xls";
                        sheetName = ABPAUFFKRS + "_" + ABPAUFFLFD + "_" + ABPAUFFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        CellName = "PrintDate";
                        value = System.DateTime.Today.ToShortDateString();
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "PageNum";
                        value = "1";
                        exAgent.SetCellValue(CellName, value, true);
                        FillInOrderPos(dsSource.Tables["TECHSAMPLE"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (strCompany == "HLT")
                {
                    HideNullRow();
                }
                else
                {
                    //HideNullRow();
                }
                exAgent.Save();
                //MessageBox.Show("你是否打开Exclel?",,MessageBoxButtons.YesNo,MessageBoxDefaultButton.Button2);

                exAgent.Close();
            }
        }
        private void HideNullRow()
        {
            return;
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);
                var val2 = exAgent.GetCellValue(1, 1);
                var val3 = exAgent.GetCellValue(1, 1);
                //Case 1
                rowstart = 4; rowend = 15; col1 = 1;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 1, 11);
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col1);
                    }
                }
                /*  rowstart = 53; rowend = 56; col1 = 2; col2 = 3;
                  for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                  {
                      val1 = exAgent.GetRangeValue(Rowi, Rowi, 9, 38);
                      if ((string)val1 == "0")

                          //.get_Value(Type.Missing);
                          if ((string)val1 == "0")
                          {
                              HidedRows++;
                              exAgent.HideRows(Rowi, col1, Rowi, col1);
                          }
                  }
                 /* rowstart = 341; rowend = 351; col1 = 17; col2 = 23;
                  for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                  {
                      val1 = exAgent.GetRangeValue(Rowi, Rowi, 3, 4);
                      val2 = exAgent.GetRangeValue(Rowi, Rowi, 21, 22);
                      if ((string)val1 == "0" && (string)val2 == "0")
                      {
                          HidedRows++;
                          exAgent.HideRows(Rowi, col1, Rowi, col2);
                      }
                  }
                  /*
                               rowstart = 381 + HidedRows - 21; rowend = 387 + HidedRows - 21; col1 = 3; col2 = 22;
                               for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                               {
                                   val1 = exAgent.GetCellValue(Rowi, col1);
                                   val2 = exAgent.GetCellValue(Rowi, col2);
                                   if (val1 == null && val2 == null)
                                   {
                                       HidedRows++;
                                       exAgent.HideRows(Rowi, col1, Rowi, col2);
                                   }
                               }
                               */
                /*  int InsertedRows = HidedRows - 22;
                  if (HidedRows > 0)
                  {
                      rowstart = 359;
                      exAgent.InsertRows(rowstart, HidedRows - 22);
                  }
                  rowstart = 433 + InsertedRows; rowend = 1800 + InsertedRows;
                  for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                  {
                      val1 = exAgent.GetRangeValue(Rowi, Rowi, 8, 34);
                      if ((string)val1 == "0")
                      {
                          if (Rowi == rowstart)
                              Rowi = Rowi - 1;
                          exAgent.HideRows(Rowi, col1, rowend, col1);
                          Rowi = rowend;
                      }
                  }*/
            }
            catch (Exception ex)
            {
               Tracer.Error(ex.StackTrace, ex); 
            }
        }
        #endregion
    }
}